package com.start.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.admin.entity.StartSysDeptJobEntity;
import com.start.admin.service.StartSysDeptJobService;
import com.start.common.api.Result;
import com.start.common.dataRule.DataQueryConstructor;
import com.start.core.annotations.RegisterMenu;
import com.start.core.annotations.RegisterPermission;
import com.start.core.annotations.StartLog;
import com.start.security.annotations.DataPermission;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 岗位管理
 *
 * @author gxl
 * @email ${email}
 * @date 2020-12-22 16:15:02
 */
@RestController
@RequestMapping("/start/sys/sysDeptJob")
@RegisterMenu(value = "岗位管理", systemCode = "sys:SysDeptJob", component = "views/sys/sysDeptJob/index", path = "/sysDeptJob/index")
public class StartSysDeptJobController {
    private static final String LOG_TITLE = "岗位管理";

    @Autowired
    private StartSysDeptJobService startSysDeptJobService;

    /**
     * 列表
     */
    @GetMapping("/page")
    @DataPermission("sys/sysDeptJob/list")
    public Result<?> list(Page<StartSysDeptJobEntity> page, StartSysDeptJobEntity entity) {
        QueryWrapper<StartSysDeptJobEntity> queryWrapper = DataQueryConstructor.createEntity(entity);
        return Result.ok(startSysDeptJobService.page(page, queryWrapper));
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @StartLog(value = LOG_TITLE, action = "查询")
    @PreAuthorize("@pas.hasPermission('sys:sysDeptJob:info')")
    @RegisterPermission(value = "sys:sysDeptJob:info", name = "查看")
    public Result<?> info(@PathVariable("id") String id) {
        return Result.ok(startSysDeptJobService.getById(id));
    }

    /**
     * 保存
     */
    @PostMapping("/add")
    @StartLog(value = LOG_TITLE, action = "添加")
    @PreAuthorize("@pas.hasPermission('sys:sysDeptJob:add')")
    @RegisterPermission(value = "sys:sysDeptJob:add", name = "添加")
    public Result<?> save(@RequestBody StartSysDeptJobEntity sysDeptJob) {
        startSysDeptJobService.save(sysDeptJob);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/updateById")
    @StartLog(value = LOG_TITLE, action = "修改")
    @PreAuthorize("@pas.hasPermission('sys:sysDeptJob:edit')")
    @RegisterPermission(value = "sys:sysDeptJob:edit", name = "修改")
    public Result<?> update(@RequestBody StartSysDeptJobEntity sysDeptJob) {
        startSysDeptJobService.updateById(sysDeptJob);
        return Result.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/deleteById/{id}")
    @StartLog(value = LOG_TITLE, action = "删除")
    @PreAuthorize("@pas.hasPermission('sys:sysDeptJob:delete')")
    @RegisterPermission(value = "sys:sysDeptJob:delete", name = "删除")
    public Result<?> delete(@PathVariable("id") String id) {
        startSysDeptJobService.removeById(id);
        return Result.ok();
    }

    @GetMapping("/getList")
    public Result<?> getList(String deptId) {
        LambdaQueryWrapper<StartSysDeptJobEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(deptId), StartSysDeptJobEntity::getDeptId, deptId);
        return Result.ok(startSysDeptJobService.list(queryWrapper));
    }

}
